/*
 * Copyright 2010-2011 CGT&DC ISA RAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package websolve.workflow.runtime.test.nested;

import websolve.workflow.runtime.Task;
import websolve.workflow.runtime.Workflow;
import websolve.workflow.runtime.test.diamond.TaskA;
import websolve.workflow.runtime.test.diamond.TaskBC;
import websolve.workflow.runtime.test.diamond.TaskD;

public class NestedWorkflow {

    public static void main(String[] args) {

        // DESCRIBE

        Workflow main = new Workflow("main");
        main.addInput("in");
        main.addOutput("out");

        Task begin = new BeginTask("Begin");
        begin.setInput("in", main.getInput("in"));
        main.addTask(begin);

        // ---- DIAMOND ----

        Workflow diamond = new Workflow("diamond");
        diamond.addInput("in");
        diamond.addOutput("out");

        diamond.setInput("in", begin.getOutput("out"));

        Task taskA = new TaskA("A");
        taskA.setInput("in", diamond.getInput("in"));
        diamond.addTask(taskA);

        Task taskB = new TaskBC("B");
        taskB.setInput("in", taskA.getOutput("out1"));
        diamond.addTask(taskB);

        Task taskC = new TaskBC("C");
        taskC.setInput("in", taskA.getOutput("out2"));
        diamond.addTask(taskC);

        Task taskD = new TaskD("D");
        taskD.setInput("in1", taskB.getOutput("out")).setInput("in2", taskC.getOutput("out"));
        diamond.addTask(taskD);

        diamond.setOutput("out", taskD.getOutput("out"));

        main.addTask(diamond);

        // ---- DIAMOND ----

        Task end = new EndTask("End");
        end.setInput("in", diamond.getOutput("out"));
        main.addTask(end);

        main.setOutput("out", end.getOutput("out"));

        // RUN

        main.setInputValue("in", "workflow");
        main.run();
        System.out.println("Result: " + main.getOutputValue("out"));

    }

}
